using UnityEngine;

public class SlotSelectorSector : EventCommander
{
	[SerializeField]
	private Transform slotHolder;

	[SerializeField]
	private UILabel label;

	[SerializeField]
	private UILabel alternativeLabel;

	[SerializeField]
	private ISlot _slot;

	[SerializeField]
	private string _slotName;

	[SerializeField]
	private GameObject defaultTexture;

	public bool selected;

	public bool viseble;

	public Animator _animator;

	private string _text;

	public Animator animator
	{
		get
		{
			if (_animator == null)
			{
				_animator = GetComponent<Animator>();
				if ((bool)_animator)
				{
					_animator.logWarnings = false;
				}
			}
			return _animator;
		}
	}

	public string text
	{
		get
		{
			if (label != null)
			{
				return label.text;
			}
			return _text;
		}
		set
		{
			if (alternativeLabel != null)
			{
				alternativeLabel.text = value;
			}
			if (label != null)
			{
				label.text = value;
			}
			else
			{
				_text = value;
			}
			if (selected)
			{
				RiseEvent(this, "ActiveSlotTextChanged", value);
			}
		}
	}

	public string slotName
	{
		get
		{
			return _slotName;
		}
		set
		{
			_slotName = value;
		}
	}

	public ISlot slot
	{
		get
		{
			return _slot;
		}
		set
		{
			_slot = value;
			if ((bool)slotHolder)
			{
				_slot.slotTransform.parent = slotHolder;
			}
			else
			{
				_slot.slotTransform.parent = base.transform;
			}
			_slot.slotTransform.localScale = Vector3.one;
			_slot.slotTransform.localPosition = Vector3.zero;
			_slot.eventCommander.AddEventListener(this, "WeaponUnloaded", delegate
			{
				_slot = null;
				selected = false;
				text = string.Empty;
				if ((bool)defaultTexture)
				{
					defaultTexture.SetActive(true);
				}
				RiseEvent(this, "SlotRemoved");
			});
			_slot.eventCommander.AddEventListener(this, "ValueChanged", delegate(Object sender, object[] args)
			{
				text = (string)args[0];
			});
			_slot.eventCommander.AddEventListener(this, "SlotSellected", delegate
			{
				Select();
			});
			_slot.eventCommander.AddEventListener(this, "AlternativeText", delegate(Object sender, object[] args)
			{
				if (!(bool)args[0])
				{
					animator.SetBool("NoAmmo", false);
					if ((bool)label)
					{
						label.gameObject.SetActive(true);
					}
					if ((bool)alternativeLabel)
					{
						alternativeLabel.gameObject.SetActive(false);
					}
				}
				else
				{
					animator.SetBool("NoAmmo", true);
				}
				if ((bool)args[2])
				{
					RiseEvent(this, "AlternativeText", (bool)args[0], (string)args[1]);
				}
			});
			_slot.eventCommander.AddEventListener(this, "WeaponVisiableChange", delegate(Object sender, object[] args)
			{
				RiseEvent(this, "SlotVisiableChange", args[0]);
			});
			_slot.eventCommander.AddEventListener(this, "StartInterfaceAnimation", delegate(Object sender, object[] args)
			{
				RiseEvent(this, "StartInterfaceAnimation", (float)args[0], (bool)args[1]);
			});
			_slot.eventCommander.AddEventListener(this, "StopInterfaceAnimation", delegate
			{
				RiseEvent(this, "StopInterfaceAnimation");
			});
		}
	}

	public override void Init()
	{
		base.Init();
		InitEvents(new string[7] { "SlotSellected", "ActiveSlotTextChanged", "SlotVisiableChange", "AlternativeText", "StartInterfaceAnimation", "SlotRemoved", "StopInterfaceAnimation" });
	}

	public override void Start()
	{
		base.Start();
	}

	public void OnPress(bool isDown)
	{
		if (viseble && !isDown)
		{
			Select();
		}
	}

	private void OnDragOver(GameObject draggedObject)
	{
		if (viseble && (bool)animator)
		{
			animator.SetBool("hover", true);
		}
	}

	private void OnDragOut(GameObject draggedObject)
	{
		if (viseble && (bool)animator)
		{
			animator.SetBool("hover", false);
		}
	}

	private void OnDrop(GameObject drag)
	{
		if (viseble)
		{
			Select();
		}
	}

	public void OnDragEnd()
	{
		if (viseble)
		{
			Select();
		}
	}

	public bool Select(bool force = false)
	{
		if ((bool)animator)
		{
			animator.SetBool("hover", false);
		}
		if (slot != null)
		{
			if (!selected || force)
			{
				selected = true;
				slot.SetActive(true);
				RiseEvent(this, "SlotSellected", slot);
			}
			else
			{
				RiseEvent(this, "SlotSellected", null);
			}
		}
		return _slot != null && _slot.Texture != null && _slot.slotTransform.parent == base.transform;
	}

	public void UnSelect()
	{
		selected = false;
		viseble = false;
		if ((bool)animator)
		{
			animator.SetBool("hover", false);
			animator.SetBool("enabled", false);
		}
	}

	public void Active(bool val)
	{
		viseble = val;
		if (_slot == null || _slot.Texture == null)
		{
			if ((bool)defaultTexture)
			{
				defaultTexture.SetActive(true);
			}
		}
		else if ((bool)defaultTexture)
		{
			defaultTexture.SetActive(false);
		}
		Collider[] componentsInChildren = GetComponentsInChildren<Collider>();
		foreach (Collider collider in componentsInChildren)
		{
			collider.enabled = val;
		}
		if ((bool)animator)
		{
			animator.SetBool("hover", false);
			animator.SetBool("enabled", val);
		}
	}
}
